package com.hongyuan.sellfitness.ui.find.view_mode;

import android.app.Application;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.find.beans.ItemCommentBean;
import com.hongyuan.sellfitness.ui.find.beans.PostCommentBean;
import com.hongyuan.sellfitness.ui.find.beans.PostDetailBean;
import com.hongyuan.sellfitness.ui.person.activity.UserInfoActivity;
import com.previewlibrary.enitity.UserViewInfo;
import java.util.ArrayList;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class PostDetailsViewModel extends CustomViewModel<MyRepository> {

    public PostDetailsViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    public ObservableField<PostDetailBean> entity = new ObservableField<>();


    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //播放视频
        public SingleLiveEvent<Void> goVideo = new SingleLiveEvent<>();
        //查看大图
        public SingleLiveEvent<Void> goLookImg = new SingleLiveEvent<>();
        //打开软键盘
        public SingleLiveEvent<Void> showEdit = new SingleLiveEvent<>();
        //关闭软键盘
        public SingleLiveEvent<Void> closeEdit = new SingleLiveEvent<>();
        //评论消息
        public SingleLiveEvent<Void> sendMessage = new SingleLiveEvent<>();
    }

    //给RecyclerView添加ObservableList
    public ObservableList<CommentItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<CommentItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_comment);

    //去用户主页
    public BindingCommand goHomePage = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("user_id",String.valueOf(entity.get().getM_id()));
            bundle.putString("userPhone",entity.get().getM_mobile());
            startActivity(UserInfoActivity.class,bundle);
        }
    });

    //打开软键盘输入评论
    public BindingCommand showEditext = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            setParams("circle_id",String.valueOf(entity.get().getCircle_id()))
                    .setParams("cr_id_father","0").setParams("first_cr_id","0");
            uc.showEdit.call();
        }
    });

    //关闭软键盘输入
    public BindingCommand closeEidt = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.closeEdit.call();
        }
    });

    //关注、取消关注
    public BindingCommand sendAttention = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if(entity.get().getIs_friend() == 1){
                //取消关注
                setParams("f_mid",String.valueOf(entity.get().getM_id())).setParams("f_type","reduce");
                requestData(Constants.ADD_FRIENT);
            }else{
                //关注
                setParams("f_mid",String.valueOf(entity.get().getM_id())).setParams("f_type","add");
                requestData(Constants.ADD_FRIENT);
            }
        }
    });

    //帖子点赞
    public BindingCommand postLike = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            setParams("circle_id",String.valueOf(entity.get().getCircle_id()));
            if(entity.get().getIs_praise() == 1){
                requestData(Constants.CANCEL_CIRCLE_PRAISE);
            }else{
                requestData(Constants.ADD_CIRCLE_PRAISE);
            }
        }
    });

    //提交评论
    public BindingCommand sendMessage = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.sendMessage.call();
        }
    });

    //单个文件查看
    public BindingCommand goLookOneFile = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if(entity.get().getCi().get(0).getCircle_type() == 2){
                uc.goVideo.call();
            }else{
                uc.goLookImg.call();
            }
        }
    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_CIRCLE_INFO){
            PostDetailBean bean = (PostDetailBean)dataBean;
            entity.set(bean);
        }

        if(code == Constants.GET_CIRCLE_REVIEWLIST){
            List<PostCommentBean.ListBean> mList = ((PostCommentBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(PostCommentBean.ListBean bean : mList){
                    CommentItemViewModel itemViewModel = new CommentItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

        if(code == Constants.ADD_FRIENT){
            if(entity.get().getIs_friend() == 1){
                entity.get().setIs_friend(0);
            }else{
                entity.get().setIs_friend(1);
            }
            entity.notifyChange();
        }

        if(code == Constants.ADD_CIRCLE_PRAISE || code == Constants.CANCEL_CIRCLE_PRAISE){

            if(itemViewModel != null){

                if(itemViewModel.entity.get().getIs_praise() == 1){
                    itemViewModel.entity.get().setIs_praise(0);
                    itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num()-1);
                }else{
                    itemViewModel.entity.get().setIs_praise(1);
                    itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num()+1);
                }

                itemViewModel.entity.notifyChange();

                //操作完毕清空下
                itemViewModel = null;
            }else{
                if(entity.get().getIs_praise() == 1){
                    entity.get().setIs_praise(0);
                    entity.get().setPraise_num(entity.get().getPraise_num()-1);
                }else{
                    entity.get().setIs_praise(1);
                    entity.get().setPraise_num(entity.get().getPraise_num()+1);
                }
                entity.notifyChange();
            }

        }

        if(code == Constants.ADD_CIRCLE_REVIEW){
            ItemCommentBean.CrInfoBean infoBean = ((ItemCommentBean)dataBean).getCr_info();
            if(itemViewModel != null){
                PostCommentBean.ListBean.ListSBean bean = new PostCommentBean.ListBean.ListSBean();
                bean.setAdd_date(infoBean.getAdd_date());
                bean.setAdd_time(infoBean.getAdd_time());
                bean.setCircle_content(infoBean.getCircle_content());
                bean.setCircle_id(infoBean.getCircle_id());
                bean.setCr_id(infoBean.getCr_id());
                bean.setCr_id_father(infoBean.getCr_id_father());
                bean.setCr_state(infoBean.getCr_state());
                bean.setFirst_cr_id(infoBean.getFirst_cr_id());
                bean.setM_id(infoBean.getM_id());
                bean.setM_mobile(infoBean.getM_mobile());
                bean.setM_name(infoBean.getM_name());
                bean.setMi_head(infoBean.getMi_head());
                bean.setPraise_num(infoBean.getPraise_num());
                bean.setReply_num(infoBean.getReply_num());
                bean.setTo_m_name(infoBean.getTo_m_name());
                bean.setTo_mid(infoBean.getTo_mid());
                bean.setUpdate_admin(infoBean.getUpdate_admin());
                bean.setUpdate_time(infoBean.getUpdate_time());
                bean.setCr_content(infoBean.getCr_content());

                itemViewModel.entity.get().getList_s().add(bean);
                itemViewModel.entity.notifyChange();
                itemViewModel.createData(bean);

                itemViewModel = null;

            }else{
                PostCommentBean.ListBean bean = new PostCommentBean.ListBean();
                bean.setAdd_date(infoBean.getAdd_date());
                bean.setAdd_time(infoBean.getAdd_time());
                bean.setCircle_content(infoBean.getCircle_content());
                bean.setCircle_id(infoBean.getCircle_id());
                bean.setCr_id(infoBean.getCr_id());
                bean.setCr_id_father(infoBean.getCr_id_father());
                bean.setCr_state(infoBean.getCr_state());
                bean.setFirst_cr_id(infoBean.getFirst_cr_id());
                bean.setM_id(infoBean.getM_id());
                bean.setM_mobile(infoBean.getM_mobile());
                bean.setM_name(infoBean.getM_name());
                bean.setMi_head(infoBean.getMi_head());
                bean.setPraise_num(infoBean.getPraise_num());
                bean.setReply_num(infoBean.getReply_num());
                bean.setTo_mid(infoBean.getTo_mid());
                bean.setUpdate_admin(infoBean.getUpdate_admin());
                bean.setUpdate_time(infoBean.getUpdate_time());
                bean.setCr_content(infoBean.getCr_content());
                bean.setList_s(new ArrayList<>());

                CommentItemViewModel itemViewModel = new CommentItemViewModel(this,bean);
                observableList.add(0,itemViewModel);
            }

        }
    }

    /*
     * 获取图片集和图片所处位置
     * */
    public List<UserViewInfo> getInfoList(List<PostDetailBean.CiBean> list, List<ImageView> mImageViewList){
        List<UserViewInfo> imgList = new ArrayList<>();
        for(int i = 0 ; i < list.size() ; i++){
            imgList.add(new UserViewInfo(list.get(i).getFile_src()));
            Rect bounds = new Rect();
            mImageViewList.get(i).getGlobalVisibleRect(bounds);
            imgList.get(i).setBounds(bounds);
        }

        return imgList;
    }

    private CommentItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);
        this.itemViewModel = (CommentItemViewModel) itemVM;
    }
}
